package com.future.cb.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.future.cb.common.exception.AppException;
import com.future.cb.dao.entity.SysUser;
import com.future.cb.dao.repository.SysUserRepository;

@Service
public class SysUserService {
	@Autowired
	private SysUserRepository sysUserRepository;

	/**
	 * 查找所有的user
	 * 
	 * @return
	 */
	public List<SysUser> findAllUsers() {
		return this.sysUserRepository.findAll();
	}

	/**
	 * 根据id查找user
	 * 
	 * @param id
	 * @return
	 */
	public SysUser findUserById(String id) {
		return this.sysUserRepository.getOne(id);
	}

	/**
	 * 检查是否存在userid
	 * 
	 * @param userid
	 */
	public void validateUserid(String userid) {
		if (this.sysUserRepository.existsById(userid)) {
			throw new AppException("用户信息不存在!");
		}

	}

	/**
	 * 校验用户名密码的正确
	 * 
	 * @param username
	 * @param password
	 */
	public void validateUser(String username, String password) {
		SysUser sysUser = this.sysUserRepository.findByUsername(username);
		if (sysUser == null) {
			throw new AppException("用户:" + username + "认证失败!");
		}
		String passwordDb = sysUser.getPass();
		if (passwordDb == null || !passwordDb.equals(password)) {
			throw new AppException("用户:" + username + "认证失败!");
		}
	}
}
